#include "includes.h"

extern char Pixel[128];
extern int coordinate,Right,Left; 
extern int setspeed,speednow,r_flag;

extern int point_flag,point_array[10],z_flag;
extern float servo_P;



float OutData[4];  //虚拟示波器用

/*************************************************************/
/*                       串口发送函数                        */
/*************************************************************/
void SCI_send(unsigned char data) 
{
  while(!SCI0SR1_TDRE);       //等待发送数据寄存器（缓冲器）为空
  SCI0DRL = data;
}

void send_int(int data) 
{
  int temp1=0,temp2=0,temp3=0,temp4=0;
  if(data>=0) 
  {
   if(data>=1000) 
   {
    temp1=data/1000;
    while(!SCI0SR1_TDRE);       //等待发送数据寄存器（缓冲器）为空
    SCI0DRL = temp1+48;
    }
   if(data>=100) 
   {
    temp2=(data%1000)/100;
    while(!SCI0SR1_TDRE);       //等待发送数据寄存器（缓冲器）为空
    SCI0DRL = temp2+48;  
   }
   if(data>=10) 
   {
    temp3=(data%100)/10;
    while(!SCI0SR1_TDRE);       //等待发送数据寄存器（缓冲器）为空
    SCI0DRL = temp3+48;  
   }
    temp4=data%10;
    while(!SCI0SR1_TDRE);       //等待发送数据寄存器（缓冲器）为空
    SCI0DRL = temp4+48;
  } 
  else 
  {
   data=-data;
   SCI_send('-');
   if(data>=1000) 
   {
    temp1=data/1000;
    while(!SCI0SR1_TDRE);       //等待发送数据寄存器（缓冲器）为空
    SCI0DRL = temp1+48;
    }
   if(data>=100) 
   {
    temp2=(data%1000)/100;
    while(!SCI0SR1_TDRE);       //等待发送数据寄存器（缓冲器）为空
    SCI0DRL = temp2+48;  
   }
   if(data>=10) 
   {
    temp3=(data%100)/10;
    while(!SCI0SR1_TDRE);       //等待发送数据寄存器（缓冲器）为空
    SCI0DRL = temp3+48;  
   }
    temp4=data%10;
    while(!SCI0SR1_TDRE);       //等待发送数据寄存器（缓冲器）为空
    SCI0DRL = temp4+48;
  }  
}

/*************************************************************/
/*                  串口发送字符串函数                       */
/*************************************************************/
void send_string(char *putchar) 
{ 
  while(*putchar!=0x00)       //判断字符串是否发送完毕
  {
   SCI_send((unsigned char)*putchar++);  
  }
}
/*
void send_int(int data)
{
  unsigned char dat_h,dat_l;
  if(data<0) 
  {
  dat_l=(uchar)(~data&0x00ff);
  dat_h=(uchar)(~data>>8);
  
  while(!SCI0SR1_TDRE);
  SCI0DRL = ('-');
  while(!SCI0SR1_TDRE);
  SCI0DRL = dat_h;
  while(!SCI0SR1_TDRE);
  SCI0DRL = dat_l;
  } 
  else 
  {
  dat_l=(unsigned char)(data&0x00ff);
  dat_h=(unsigned char)(data>>8);
  
  while(!SCI0SR1_TDRE);
  SCI0DRL = ('+');
  while(!SCI0SR1_TDRE);
  SCI0DRL = dat_h;
  while(!SCI0SR1_TDRE);
  SCI0DRL = dat_l;
  }
} */

//虚拟示波器

unsigned short CRC_CHECK(unsigned char *Buf, unsigned char CRC_CNT)
{
    unsigned short CRC_Temp;
    unsigned char i,j;
    CRC_Temp = 0xffff;

    for (i=0;i<CRC_CNT; i++){      
        CRC_Temp ^= Buf[i];
        for (j=0;j<8;j++) {
            if (CRC_Temp & 0x01)
                CRC_Temp = (CRC_Temp >>1 ) ^ 0xa001;
            else
                CRC_Temp = CRC_Temp >> 1;
        }
    }
    return(CRC_Temp);
}

void OutPut_Data(void)
{
  int temp[4] = {0};
  unsigned int temp1[4] = {0};
  unsigned char databuf[10] = {0};
  unsigned char i;
  unsigned short CRC16 = 0;
  for(i=0;i<4;i++)
   {
    
    temp[i]  = (int)OutData[i];
    temp1[i] = (unsigned int)temp[i];
    
   }
   
  for(i=0;i<4;i++) 
  {
    databuf[i*2]   = (unsigned char)(temp1[i]%256);
    databuf[i*2+1] = (unsigned char)(temp1[i]/256);
  }
  
  CRC16 = CRC_CHECK(databuf,8);
  databuf[8] = CRC16%256;
  databuf[9] = CRC16/256;
  
  for(i=0;i<10;i++)
  SCI_send(databuf[i]);
}

void SendHex(unsigned char hex)        //CCD发送图像
{
  unsigned char temp;
  temp = hex >> 4;
  if(temp < 10) {
    SCI_send(temp + '0');
  } else {
    SCI_send(temp - 10 + 'A');
  }
  temp = hex & 0x0F;
  if(temp < 10) {
    SCI_send(temp + '0');
  } else {
    SCI_send(temp - 10 + 'A');
  }
}

void SendImageData(unsigned char * ImageData) 
{
    unsigned char i;
    unsigned char crc = 0;

    /* Send Data */
    SCI_send('*');
    SCI_send('L');
    SCI_send('D');

    SendHex(0);
    SendHex(0);
    SendHex(0);
    SendHex(0);

    for(i=0; i<128; i++) {
      SendHex(*ImageData++);
    }

    SendHex(crc);
    SCI_send('#');
}

void Buletooth(void) 
{
  int i;
  OutData[0]=coordinate;
  OutData[1]=speednow;
  OutData[2]=setspeed;
  OutData[3]=servo_P;
  //OutPut_Data();
    
 // SendImageData(Pixel);    //发送CCD上位机图像
    
    send_string("s:");
    send_int(speednow);                           
    send_string("  c:");
    send_int(coordinate);
    send_string("  L R:");
    send_int(Left);
    SCI_send(' ');
    send_int(Right);
    send_string("  p_:");
    for(i=0;i<point_flag;i++) 
    {
      send_int(point_array[i]);
      SCI_send(' ');
    }
    send_string(" zf:");
    
    SCI_send('\n');     /*    */
}
            